<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'physical:exercisePrescriptionTrigger:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ triggerId: record.triggerId })">
          {{ record.triggerType }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsPhysicalExercisePrescriptionTriggerList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    exercisePrescriptionTriggerDelete,
    exercisePrescriptionTriggerListData,
    exercisePrescriptionSelectData,
    sportsPhysicalSkillsSelectDataByPrescription,
  } from '/@/api/physical/exercisePrescriptionTrigger';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  const { t } = useI18n('physical.exercisePrescriptionTrigger');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('运动处方库触发管理'),
  };

  const param = ref({ prescriptionId: 0, triggerType: '' });
  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('规则类型'),
        field: 'triggerType',
        component: 'Select',
        componentProps: {
          dictType: 's_quality_type',
          allowClear: true,
          onChange: (e) => {
            console.log(e);
            param.value.triggerType = e ? e : '';
          },
        },
      },
      {
        label: t('运动处方'),
        field: 'prescriptionId',
        component: 'Select',
        componentProps: {
          api: async () => {
            let res = await exercisePrescriptionSelectData({});
            let data = [];
            res.data.forEach((item, index) => {
              data.push({ label: item.prescriptionName, value: item.prescriptionId });
            });
            return data;
          },
          dictType: '',
          allowClear: true,
          onChange: (e) => {
            param.value.prescriptionId = e ? e : 0;
            console.log(param.value);
          },
        },
      },
      {
        label: t('项名称'),
        field: 'evaluationCheckTerm',
        component: 'Select',
        componentProps: {
          api: sportsPhysicalSkillsSelectDataByPrescription,
          params: param.value,
          immediate: false,
          resultField: 'data',
          allowClear: true,
        },
      },
      {
        label: t('警戒分值1'),
        field: 'triggerScore1',
        component: 'Input',
      },
      {
        label: t('警戒分值2'),
        field: 'triggerScore2',
        component: 'Input',
      },
      {
        label: t('分值符号'),
        field: 'scoreSymbol',
        component: 'Select',
        componentProps: {
          dictType: 's_scoring_rules',
          allowClear: true,
        },
      },
      // {
      //   label: t('状态'),
      //   field: 'status',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'sys_search_status',
      //     allowClear: true,
      //     onChange: handleSuccess,
      //   },
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('规则类型'),
      dataIndex: 'triggerType',
      key: 'a.trigger_type',
      sorter: true,
      width: 230,
      align: 'left',
      dictType: 's_quality_type',
    },
    {
      title: t('警戒分值1'),
      dataIndex: 'triggerScore1',
      key: 'a.trigger_score_1',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('警戒分值2'),
      dataIndex: 'triggerScore2',
      key: 'a.trigger_score_2',
      sorter: true,
      align: 'center',
      width: 130,
    },
    {
      title: t('分值符号'),
      dataIndex: 'scoreSymbol',
      key: 'a.score_symbol',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 's_scoring_rules',
    },
    {
      title: t('运动处方'),
      dataIndex: 'exercisePrescription.prescriptionName',
      key: 'a.prescription_id',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: '',
    },
    {
      title: t('项名称'),
      dataIndex: 'projectName',
      key: 'a.evaluation_check_term',
      sorter: true,
      width: 130,
      align: 'center',
    },
    // {
    //   title: t('状态'),
    //   dataIndex: 'status',
    //   key: 'a.status',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'sys_search_status',
    // },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑运动处方库触发'),
        onClick: handleForm.bind(this, { triggerId: record.triggerId }),
        auth: 'physical:exercisePrescriptionTrigger:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除运动处方库触发'),
        popConfirm: {
          title: t('是否确认删除运动处方库触发'),
          confirm: handleDelete.bind(this, { triggerId: record.triggerId }),
        },
        auth: 'physical:exercisePrescriptionTrigger:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: exercisePrescriptionTriggerListData,
    beforeFetch: (params) => {
      return params;
    },
    afterFetch: (res) => {
      res.forEach((item) => {
        if (item.triggerType == 2) {
          item.projectName = item.sportsPhysicalSkills.termName;
        } else {
          item.projectName = item.sportsWeight.termName;
        }
      });
      return res;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await exercisePrescriptionTriggerDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
